package flc.ast.dialog;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.SeekBar;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;

import flc.ast.R;
import flc.ast.databinding.DialogColorBinding;
import stark.common.basic.base.BaseSmartDialog;

public class ColorDialog extends BaseSmartDialog<DialogColorBinding> implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {

    private static final String COLOR_STRING_FORMAT = "%02x%02x%02x";
    private int red, green, blue;

    public ColorDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_color;
    }

    @Override
    protected void initView(View view) {
        red = 255;
        green = 0;
        blue = 0;


        mDataBinding.redSeekBar.setProgress(red);
        mDataBinding.greenSeekBar.setProgress(green);
        mDataBinding.blueSeekBar.setProgress(blue);

        mDataBinding.llColorView.setBackgroundColor(Color.rgb(red, green, blue));

        mDataBinding.etColorContent.setText(String.format(COLOR_STRING_FORMAT, red, green, blue));

        mDataBinding.ivColorCancel.setOnClickListener(this);
        mDataBinding.ivColorConfirm.setOnClickListener(this);
        mDataBinding.redSeekBar.setOnSeekBarChangeListener(this);
        mDataBinding.greenSeekBar.setOnSeekBarChangeListener(this);
        mDataBinding.blueSeekBar.setOnSeekBarChangeListener(this);
        mDataBinding.etColorContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() == 7) {
                    mDataBinding.llColorView.setBackgroundColor(Color.parseColor("#" + String.format(COLOR_STRING_FORMAT, red, green, blue)));
                }
            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivColorConfirm:
                String content = mDataBinding.etColorContent.getText().toString();
                if (TextUtils.isEmpty(content)) {
                    ToastUtils.showShort(R.string.et_color_tips);
                    return;
                }
                if (content.length() < 6) {
                    ToastUtils.showShort(R.string.color_tips2);
                    return;
                }
                dismiss();
                if (listener != null) {
                    listener.onClickConfirm("#" + content);
                }
                break;
            case R.id.ivColorCancel:
                dismiss();
                break;
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
        if (seekBar.getId() == R.id.redSeekBar) {
            red = progress;
            mDataBinding.redToolTip.setText(red + "");
        } else if (seekBar.getId() == R.id.greenSeekBar) {
            green = progress;
            mDataBinding.greenToolTip.setText(green + "");
        } else if (seekBar.getId() == R.id.blueSeekBar) {
            blue = progress;
            mDataBinding.blueToolTip.setText(blue + "");
        }
        mDataBinding.llColorView.setBackgroundColor(Color.rgb(red, green, blue));
        mDataBinding.etColorContent.setText(String.format(COLOR_STRING_FORMAT, red, green, blue));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    private IListener listener;

    public void setListener(IListener listener) {
        this.listener = listener;
    }

    public interface IListener {
        void onClickConfirm(String color);
    }
}
